package com.metel.mservice.common.constants;

import javax.ws.rs.core.Response;

/**
 * Commonly used status codes defined by HTTP.<br>
 * <br>
 * 
 * @see <code>javax.ws.rs.core.Response</code>
 * @author sr80.choi
 */
public enum HttpStatus {

	OK(200, "OK"),

	CREATED(201, "Created"),

	ACCEPTED(202, "Accepted"),

	NO_CONTENT(204, "No Content"),

	MOVED_PERMANENTLY(301, "Moved Permanently"),

	SEE_OTHER(303, "See Other"),

	NOT_MODIFIED(304, "Not Modified"),

	TEMPORARY_REDIRECT(307, "Temporary Redirect"),

	BAD_REQUEST(400, "Bad Request"),

	UNAUTHORIZED(401, "Unauthorized"),

	FORBIDDEN(403, "Forbidden"),

	NOT_FOUND(404, "Not Found"),
	
	Method_Not_Allowed(405, "Method Not Allowed"),

	NOT_ACCEPTABLE(406, "Not Acceptable"),

	CONFLICT(409, "Conflict"),

	GONE(410, "Gone"),

	PRECONDITION_FAILED(412, "Precondition Failed"),

	UNSUPPORTED_MEDIA_TYPE(415, "Unsupported Media Type"),

	INTERNAL_SERVER_ERROR(500, "Internal Server Error"),

	SERVICE_UNAVAILABLE(503, "Service Unavailable"),
	
	SERVER_IS_BUSY(400, "Server is Busy");

	private Integer status;

	private String name;

	private HttpStatus(Integer code, String name) {
		this.status = code;
		this.name = name;
	}

	public Integer getStatusCode() {
		return status;
	}

	@Override
	public String toString() {
		return name;
	}
	
	/**
	 * Convert instance <br>
	 * <p>
	 * <blockquote>
	 * <pre>
	 * from: <code>com.samsungchaton.common.constants.HttpStatus</code>
	 * to: <code>javax.ws.rs.core.Response</code>
	 * </pre>
	 * </blockquote>
	 * <p>
	 * 
	 * @return
	 */
	public Response toResponse() {
		return Response.status(status).build();
	}

	/**
	 * Convert instance <br>
	 * <p>
	 * <blockquote>
	 * <pre>
	 * from: <code>javax.ws.rs.core.Response</code> 
	 * to: <code>com.samsungchaton.common.constants.HttpStatus</code>
	 * </pre>
	 * </blockquote>
	 * <p>
	 * 
	 * @param response
	 * @return
	 */
	public static HttpStatus fromResponse(Response response) {
		HttpStatus httpStatus = null;

		int targetStatus = response.getStatus();
		HttpStatus[] values = values();
		for (int i = 0; i < values.length; i++) {
			HttpStatus value = values[i];

			if (targetStatus == value.getStatusCode()) {
				httpStatus = value;
				break;
			}
		}

		return httpStatus;
	}
}
